package com.ruoyi.web.controller.blog;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.blog.domain.TComment;
import com.ruoyi.blog.service.ITCommentService;

/**
 * 评论Controller
 * 
 * @author yang
 * @date 2021-11-15
 */
@RestController
@RequestMapping("/blog/comment")
public class TCommentController extends BaseController
{
    @Autowired
    private ITCommentService tCommentService;

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TComment tComment)
    {
        startPage();
        List<TComment> list = tCommentService.selectTCommentList(tComment);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:export')")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TComment tComment) throws IOException
    {
        List<TComment> list = tCommentService.selectTCommentList(tComment);
        ExcelUtil<TComment> util = new ExcelUtil<TComment>(TComment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return AjaxResult.success(tCommentService.selectTCommentByCommentId(commentId));
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:add')")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TComment tComment)
    {
        return toAjax(tCommentService.insertTComment(tComment));
    }

    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TComment tComment)
    {
        return toAjax(tCommentService.updateTComment(tComment));
    }

    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:remove')")
    @Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(tCommentService.deleteTCommentByCommentIds(commentIds));
    }

    /**
     * 新增评论
     */
    @PostMapping("/fromIndex/addcomment")
    public AjaxResult addFromIndex(@RequestBody TComment tComment)
    {
        int res = 0;
        if(SecurityUtils.getLoginUser() == null){
            res = tCommentService.insertTComment(tComment);
        }else{
            LoginUser loginUser = SecurityUtils.getLoginUser();
            tComment.setUserId(loginUser.getUserId());
            tComment.setUserName(loginUser.getUsername());
            res = tCommentService.insertTComment(tComment);
        }
        return toAjax(res);
    }

    /**
     * 查询评论列表
     */
    @GetMapping("/fromIndex/list")
    public TableDataInfo listFromIndex(TComment tComment)
    {
        startPage();
        List<TComment> list = tCommentService.selectTCommentList(tComment);
        return getDataTable(list);
    }
}
